<?php
namespace app\api\controller;

use app\admin\model\Ota;
use app\admin\model\OtaCon;
use app\admin\model\OtaGroup;
use app\admin\model\OtaImei;
use app\admin\model\OtaUser;
use think\Db;
use think\Request;
use think\response\Json;

class DtuController extends Controller
{
    protected $authExcept = [
        'hezhou','custom','ota','getparam'
    ];

    public function getparam(Request $request)
    {
        $param           = $request->param();
        if (!isset($param['imei']) || empty($param['imei'])){
            return api_error('参数异常');
        }
        $api_url = 'http://dtu.openluat.com/api/site/device/'.$param['imei'].'/param';
        $data = sendRequest($api_url);
        return $data;
    }

    //合宙
    public function hezhou(Request $request, Ota $model, OtaGroup $otaGroup, OtaUser $otaUser, OtaCon $otaCon)
    {
        $param           = $request->param();
        if (!isset($param['imei']) || empty($param['imei'])){
            return api_error('参数异常');
        }

        $ota = $model->where(['ota_name'=>$param['imei']])->find();

        $group = $otaGroup->where(['id'=>$ota['ota_group']])->find()->toArray();

        $otaUser = $otaUser->where(['account'=>$ota['ota_account']])->find();

        if ($otaUser['balance'] <= 0 || $otaUser['balance'] - $otaUser['request_fee'] < 0){
            return api_error('余额不足');
        }

        $balance = $otaUser['balance'] - $otaUser['request_fee'];

        $otaConCount = $otaCon->where(['con_account'=>$ota['ota_account'],'con_group'=>$ota['ota_group']])->count();

        if ($otaConCount > 0){

            $ota_con = [
                'con_balance' => $otaUser['request_fee'],
                'con_group' => $ota['ota_group'],
                'con_account' => $ota['ota_account'],
                'request_num' => $otaConCount+1,
                'create_time' => time(),
            ];

        }else {

            $ota_con = [
                'con_balance' => $otaUser['request_fee'],
                'con_group' => $ota['ota_group'],
                'con_account' => $ota['ota_account'],
                'request_num' => 1,
                'create_time' => time(),
            ];
        }


        Db::startTrans();

        $otaUserRes = $otaUser->where(['account'=>$ota['ota_account']])->update(['balance'=>$balance]);

        $otaConRes = $otaCon::create($ota_con);

        if ($otaUserRes && $otaConRes){
            Db::commit();
            return $group ? !empty($group['config_hezhou']) ? $group['config_hezhou'] : api_error('暂无数据','',404) : api_error('获取失败');
        }else{
            Db::rollback();
            return api_error('获取失败');
        }
    }

    //自定义
    public function custom(Request $request, Ota $model, OtaGroup $otaGroup, OtaUser $otaUser, OtaCon $otaCon)
    {
        $param           = $request->param();
        if (!isset($param['imei']) || empty($param['imei'])){
            return api_error('参数异常');
        }

        $ota = $model->where(['ota_name'=>$param['imei']])->find();

        $group = $otaGroup->where(['id'=>$ota['ota_group']])->find();

        $otaUser = $otaUser->where(['account'=>$ota['ota_account']])->find();

        if ($otaUser['balance'] <= 0 || $otaUser['balance'] - $otaUser['request_fee'] < 0){
            return api_error('余额不足');
        }

        $balance = $otaUser['balance'] - $otaUser['request_fee'];

        $otaConCount = $otaCon->where(['con_account'=>$ota['ota_account'],'con_group'=>$ota['ota_group']])->count();

        if ($otaConCount > 0){

            $ota_con = [
                'con_balance' => $otaUser['request_fee'],
                'con_group' => $ota['ota_group'],
                'con_account' => $ota['ota_account'],
                'request_num' => $otaConCount+1,
                'create_time' => time(),
            ];

        }else {

            $ota_con = [
                'con_balance' => $otaUser['request_fee'],
                'con_group' => $ota['ota_group'],
                'con_account' => $ota['ota_account'],
                'request_num' => 1,
                'create_time' => time(),
            ];
        }


        Db::startTrans();

        $otaUserRes = $otaUser->where(['account'=>$ota['ota_account']])->update(['balance'=>$balance]);

        $otaConRes = $otaCon::create($ota_con);

        if ($otaUserRes && $otaConRes){
            Db::commit();
            return $group ?  !empty($group['config_custom']) ? $group['config_custom'] : api_error('暂无数据','',404) : api_error('获取失败');
        }else{
            Db::rollback();
            return api_error('获取失败');
        }
    }

    //OTA
    public function ota(Request $request, Ota $model, OtaGroup $otaGroup, OtaImei $otaImei , OtaUser $otaUser, OtaCon $otaCon): Json
    {
        $param           = $request->param();
        if (!isset($param['imei']) || empty($param['imei'])){
            return api_error('参数异常');
        }

        $ota = $model->where(['ota_name'=>$param['imei']])->find();

        $group = $otaGroup->where(['id'=>$ota['ota_group']])->find();

        $otaUser = $otaUser->where(['account'=>$ota['ota_account']])->find();

        if ($otaUser['balance'] <= 0 || $otaUser['balance'] - $otaUser['request_fee'] < 0){
            return api_error('余额不足');
        }

        $balance = $otaUser['balance'] - $otaUser['request_fee'];

        $otaUser->where(['account'=>$ota['ota_account']])->update(['balance'=>$balance]);

        $otaConCount = $otaCon->where(['con_account'=>$ota['ota_account'],'con_group'=>$ota['ota_group']])->count();

        if ($otaConCount > 0){

            $ota_con = [
                'con_balance' => $otaUser['request_fee'],
                'con_group' => $ota['ota_group'],
                'con_account' => $ota['ota_account'],
                'request_num' => $otaConCount+1,
                'create_time' => time(),
            ];

        }else {

            $ota_con = [
                'con_balance' => $otaUser['request_fee'],
                'con_group' => $ota['ota_group'],
                'con_account' => $ota['ota_account'],
                'request_num' => 1,
                'create_time' => time(),
            ];
        }


        Db::startTrans();

        $otaUserRes = $otaUser->where(['account'=>$ota['ota_account']])->fetchSql()->update(['balance'=>$balance]);

        $otaConRes = $otaCon::create($ota_con);

        if ($otaUserRes && $otaConRes){
            Db::commit();
            $iemi = $otaImei->where(['ota_group'=>$ota['ota_group'],'ota_account'=>$ota['ota_account']])->order('create_time','desc')->limit(1)->find();
            if ($iemi){
                $otaImei->where(['ota_group'=>$ota['ota_group'],'ota_account'=>$ota['ota_account']])->update(['up_num'=>$iemi['up_num']+1]);
                $iemi = json_decode(json_encode($iemi),true);
                $data = [
                    'state' => (count($iemi) > 0) ? 1 : 0,
                    'name'  => $iemi['name'],
                    'edition' => $iemi['version'],
                    'grouping' => $group['group_name'],
                    'download' => request()->domain().$iemi['up_pk']
                ];
            }else{
                $data = [];
            }
            if (!empty($data)) {
                return json($data);
            }else{
                return api_error(' 暂无数据','',404);
            }
        }else{
            Db::rollback();
            return api_error('获取失败');
        }
    }
}